package com.wg.wuge.tulin.jmm;

public class VoliateVisableSample {

    volatile boolean initFlag = false;

    public void save() {
        this.initFlag = true;
        String threadname = Thread.currentThread().getName();
        System.out.println("线程name : " + threadname + " 修改完成threadName 的值");

    }

    public void load() {
        String threaName = Thread.currentThread().getName();
        while (!initFlag) {

        }
        System.out.println("线程那么;" + threaName + "find init value change");
    }

    public static void main(String[] args) {
        VoliateVisableSample sample = new VoliateVisableSample();

        Thread A = new Thread(() -> {
            sample.save();
        }, "threadA");
        Thread B = new Thread(() -> {
            sample.load();
        }, "threadB");

        B.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        A.start();
    }
}
